# 登录 # 注册
import os
import re
from db import read_data
from lib import creat_code, encrypt
from conf import settings

login_user_dict = {
    'username': '',
    'bank_id': '',
    'pay_pwd': '',
    'is_admin': False,
}


# 不会改变的可以放在登录字典中


def check_login(func):
    def inner(*args, **kwargs):
        if login_user_dict['username'] == '':
            return "请先登录"
        else:
            return func(*args, **kwargs)

    return inner


def check_admin(func):
    def inner(*args, **kwargs):
        if not login_user_dict['is_admin']:
            return "当前功能只有管理员可以使用！"
        else:
            return func(*args, **kwargs)

    return inner


def init_login_dict(username):
    flag, data = read_data(tag='info', path=f'{username}_info.json')
    user_data = data[username]
    login_user_dict['username'] = user_data['username']
    login_user_dict['bank_id'] = user_data['bank_id']
    login_user_dict['pay_pwd'] = user_data['pay_pwd']
    login_user_dict['is_admin'] = user_data['is_admin']


# 获得用户名，密码
def get_username_pwd():
    for i in range(2):
        username = input("请输入用户名:>>>").strip()
        if not username.isidentifier():
            print("请不要输入非法字符！")
            continue
        password = input("请输入密码:>>>").strip()
        if len(password) > 10:
            print("密码不要超过10位！")
            continue
        break
    else:
        return False, '输入次数已使用完毕,请重新进入系统!'
    return username, password


def get_bank_id(tag, username=''):
    bank_id = input("请输入个人银行卡号(6位数字):").strip()
    if tag == 'old':
        flag, user_data = read_data(tag='info', path=f"{username}_info.json")
        if not flag:
            return flag, user_data
        if bank_id != user_data[username]['bank_id']:
            return False, "请输入正确的银行卡号!"
        else:
            return True, "银行卡号验证通过!"
    elif tag == 'new':
        re_pattern = re.compile(r'^\d{6}$')
        res = re.findall(pattern=re_pattern, string=bank_id)
        if not res:
            return False, '格式有误!'
        return True, bank_id


def get_pay_pwd():
    pay_pwd = input("请输入您的交易密码:").strip()
    pay_pwd = encrypt(pay_pwd, salt=creat_code(4))
    return pay_pwd


def get_user_info(tag=''):
    data_dict = {}
    username, password = get_username_pwd()
    if f"{username}_info.json" in os.listdir(settings.USER_INFO_DIR) and tag != 'renew':
        return False, "请不要重复注册"
    if not username:
        return username, password
    password = encrypt(password, salt=creat_code(4))
    if username == 'lea4ning':
        is_admin = True
    else:
        is_admin = False
    flag, bank_id = get_bank_id('new')
    if not flag:
        return flag, bank_id
    pay_pwd = get_pay_pwd()
    data_dict[username] = {'username': username, 'password': password,
                           'is_admin': is_admin, 'bank_id': bank_id, 'pay_pwd': pay_pwd,
                           'balance': 1000, 'shop_cart': {}}
    return data_dict, username
